package com.example.zqg.mytimedemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private int windowWidth;
    private int windowHeight;
    private GestureDetector mGestureDetector;
    private TextView textView;
    private TextView currentPosition;
    private RelativeLayout touchView;
    List<Integer> years = new ArrayList<>();
    RecyclerView yearRcView;
    CommonAdapter<Integer> yearAdapter;

    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initdatas();
        initUI();

    }

    private void initdatas() {
        for (int i = 1970; i < 2020; i++) {
            years.add(i);
        }

    }

    private void initUI() {
        textView = (TextView) findViewById(R.id.textView);
        touchView = (RelativeLayout) findViewById(R.id.touchView);
        touchView.setOnTouchListener(listenner);
        yearRcView = (RecyclerView) findViewById(R.id.yearView);
        currentPosition= (TextView) findViewById(R.id.currentPosition);
        getGestureDetector();
        reSetWindowSize();
        LinearLayoutManager mManager = new LinearLayoutManager(this);
        mManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        LinearLayoutManager mManager = new LinearLayoutManager(mContent, LinearLayoutManager.VERTICAL, false);//垂直布局，true表示左右翻转，false不翻转
        yearRcView.setLayoutManager(mManager);
        yearAdapter = new CommonAdapter<Integer>(this, R.layout.item_year, years) {
            @Override
            protected void convert(ViewHolder holder, Integer integer, int position) {
                holder.setText(R.id.tv_item1, "--" + integer + "--");
            }
        };
        yearRcView.setAdapter(yearAdapter);
//        yearRcView.getpo
        yearRcView.scrollToPosition(position);
        yearRcView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LogUtils.d("newState=="+newState);
//                position = newState;
//                currentPosition.setText("当前位置："+position);

                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    //获取最后一个可见view的位置
                    int lastItemPosition = linearManager.findLastVisibleItemPosition();
                    //获取第一个可见view的位置
                    int firstItemPosition = linearManager.findFirstVisibleItemPosition();
                    position=firstItemPosition;
                    currentPosition.setText("当前位置："+position);
//                    if (foodsArrayList.get(firstItemPosition) instanceof Foods) {
//                        int foodTypePosion = ((Foods) foodsArrayList.get(firstItemPosition)).getFood_stc_posion();
//                        FoodsTypeListview.getChildAt(foodTypePosion).setBackgroundResource(R.drawable.choose_item_selected);
//                    }
                    System.out.println(lastItemPosition + "   " + firstItemPosition);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
//        yearRcView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
//
//            }
//        });
    }


    private void reSetWindowSize() {
        Display disp = getWindowManager().getDefaultDisplay();
        windowWidth = disp.getWidth();
        windowHeight = disp.getHeight();
    }

    private class MyGestureListener extends SimpleOnGestureListener {
//        /**
//         * 双击
//         */
//        @Override
//        public boolean onDoubleTap(MotionEvent e) {
////            if (videoView2 != null) {
////                tongleAd(isPlaying);
////                if (isPlaying()) {
////                    videoView2.onPause();
////                    isPlaying = false;
////                } else {
////                    videoView2.onResume();
////                    isPlaying = true;
////                }
////            }
//            LogUtils.d("onDoubleTap");
//            return true;
//        }
//
//        @Override
//        public boolean onSingleTapConfirmed(MotionEvent e) {
//
////            singleTapController();
//            LogUtils.d("VODplayerLayout");
//            return super.onSingleTapConfirmed(e);
//        }
//
//        @Override
//        public boolean onSingleTapUp(MotionEvent e) {
//            LogUtils.d("onSingleTapUp");
//            return super.onSingleTapUp(e);
//        }

        /**
         * 滑动
         */
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            try {
                LogUtils.d("onScroll");
//                float mOldX = e1.getX(), mOldY = e1.getY();
//                int y = (int) e2.getRawY();
//                int x = (int) e2.getRawX();
//                if (windowWidth==0) {
//                    reSetWindowSize();
//                }
//                LogUtils.d("mOldY - y=="+(mOldY - y));
                if (Math.abs(distanceY) > 20) {


                    LogUtils.d("distanceY" + distanceY);
//                LogUtils.d("(mOldX - x=="+(mOldX - x));
//                LogUtils.d("windowWidth=="+windowWidth+"windowHeight=="+windowHeight);

                    MainActivity.this.onScroll(distanceY);
                }
//                if (Math.abs((mOldX - x)) < windowWidth * 0.1
//                    && Math.abs((mOldY - y)) > windowHeight * 0.1) {// 左右滑动长度不超过屏幕的十分之一
//                if ( Math.abs((mOldY - y)) > windowHeight * 0.1) {// 左右滑动长度不超过屏幕的十分之一
//                    MainActivity.this.onScroll(mOldY - y);
////                    if (mOldX > windowWidth * 4.0 / 5)// 右边滑动
////                        onVolumeSlide((mOldY - y) / windowHeight);
////                    else if (mOldX < windowWidth / 5.0)// 左边滑动
////                        onBrightnessSlide((mOldY - y) / windowHeight);
//                    return false;
//                } else {
//                    return false;
//                }
            } catch (Exception e) {
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

    }


    View.OnTouchListener listenner = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mGestureDetector.onTouchEvent(event);
            return true;
//          return   mGestureDetector.onTouchEvent(event);  //  不能 return 这个  监听不到 手势 的 onscroll 和 onfling
//            LogUtils.d("onTouch--"+mGestureDetector);
//            boolean a = mGestureDetector.onTouchEvent(event);
//            LogUtils.d("onToucha==="+a);
//            if (mGestureDetector != null && a) {
//                return true;
//            }
//
//            LogUtils.d("onTouch--event.getAction()="+event.getAction());
//            LogUtils.d("onTouch--event.getAction()="+(event.getAction() & MotionEvent.ACTION_MASK));
//            // 处理手势结束
//            switch (event.getAction() & MotionEvent.ACTION_MASK) {
//                case MotionEvent.ACTION_UP:
//                    endGesture();
//                    break;
//            }
//
//
//            return false;
        }
    };

    private void endGesture() {
        LogUtils.d("endGesture");  //这里 可以做 手势 结束的操作
    }

    private void onScroll(float data) {
        int a = (int) (data / 20);
        LogUtils.d("a==" + a);
        position = position + a;
        if (position <= 0) {
            position = 0;
        }
        if (position >= years.size()) {
            position = years.size();
        }
        yearRcView.scrollToPosition(position);
        currentPosition.setText("当前位置"+position);
        textView.setText("变化了" + a);


    }

    public GestureDetector getGestureDetector() {
        if (mGestureDetector == null) {
            mGestureDetector = new GestureDetector(this,
                new MyGestureListener());
        }
        return mGestureDetector;
    }
}
